console.log('background is running')
import { getIPInfo, setItem } from '../contentScript/utils'
let token = ''

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  console.log('request', request)

  if (request.action === 'COUNT') {
    console.log('background has received a message from popup, and count is ', request?.count)
  }

  if (request.action === 'kk-score') {
    // 向当前标签页的 content script 发送消息

    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
      chrome.tabs.sendMessage(tabs[0].id, { action: 'kkScore', data: request.data })
    })
  }
})

chrome.runtime.onInstalled.addListener(async () => {
  getIPInfo().then((res) => {
    console.log('res', res)
    if (res && res.code == 0) {
      setItem('ipinfo', res.data)
    }
  })
})

// chrome.webRequest.onBeforeRequest.addListener(
//   (details) => {
//     // 如果是 GET 请求，解析查询参数
//     const url = new URL(details.url)
//     console.log('url: ', url);
//     if (url.search) {
//       const params = url.searchParams
//       const method = params.get('method')
//       console.log('method: ', method);
//       if (method) {
//         setItem('innmethod', method)
//       }
//     }
//   },
//   {
//     urls: ['*://*.innovatemr.net/*'],
//   }, // 监听所有 URL 请求
//   ['requestBody'], // 包括请求体（POST 请求时需要）
// )
